package com.woniuxy.classteacher.uitility;

import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.client.Client;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Component;

/**
 * @author 李英勇
 * @date 2020/7/20 - 19:19
 */
@Component
public class EsUtil {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;    //用于操作es

    /**
     * 判断指定的索引名是否存在
     * @param indexName 索引名
     * @return  存在：true; 不存在：false;
     */
    public boolean isExistsIndex(String indexName){
        IndicesExistsResponse  response =
                elasticsearchTemplate.getClient().admin().indices().exists(
                        new IndicesExistsRequest().indices(new String[]{indexName})).actionGet();
        System.out.println(response.isExists());
        return response.isExists();
    }
}
